(function($){
	
	var menu = {
		init: function(params){
			return (function(){
				menu.getData(params);
				menu.createMenu(params);
			})();
		},
		getData: function(params){
			return (function(){
				if(params.data != null && params.data != undefined){
					return ;
				}
				$.ajax({
					type: "POST",
					data: {},
					url: params.url,
					async: false,
					success: function(data){
						params.data = jQuery.parseJSON(data);
					}
				});
			})();
		},
		createDocument: function(json,params){
			
			var _parentDiv = document.createElement("div");
			var _childrenDiv = document.createElement("div")
			var _div = document.createElement("div");
			var _clearDiv = document.createElement("div");
			
			_parentDiv.setAttribute("class","top_menu");
			_parentDiv.setAttribute("style","color:#337ab7;margin-left:15px;");
			_childrenDiv.setAttribute("class","top_item nav");
			_childrenDiv.setAttribute("data-mark","true");
			_childrenDiv.setAttribute("id",json.id);
			_childrenDiv.setAttribute("style","height:40px;line-height:40px;margin-top:0px;margin-bottom:0px;");
			_childrenDiv.textContent = json.text;
		
			if(!jQuery.isEmptyObject(json.params) ){
				menu.setParams(_childrenDiv,json.params);
			}
			if(!jQuery.isEmptyObject(json.url)){
				$(_childrenDiv).attr('url',json.url);
			}
			$(_childrenDiv).on("click",function(){
				menu.itemClick(this,params);
				params.backFunction(this);
				
			});
			$(_childrenDiv).on("mouseover",function(){
				if(!$(this).is(".menu-active")){
					$(this).css("background","#EEEEEE");
				}
			});
			$(_childrenDiv).on("mouseout",function(){
				if(!$(this).is(".menu-active")){
					$(this).css("background","");
				}
			});
			_parentDiv.appendChild(_childrenDiv);
			return _parentDiv;
		},
		createItemDocument: function(arr,leftNum,params){
			var _ul = document.createElement("ul");
				_ul.setAttribute("class","nav item_menu");
				_ul.setAttribute("style","display: none;margin-left:"+leftNum+"px;cursor: pointer;border-left:1px solid #428bca;");
			for(var i = 0;i < arr.length;i++){
				var jsonData = arr[i];
				var _li = document.createElement("li");
				var _a = document.createElement("a");
				var _div = document.createElement("div");
				var _clearDiv = document.createElement("div");
				_li.setAttribute("class","item_menu");
				_li.setAttribute("id",jsonData.id);
				if(jsonData.url != "" && jsonData.url != undefined){
					_a.setAttribute("href","#");
					_a.setAttribute("a-href",jsonData.url);
				}
				_a.setAttribute("href","#");
				_a.setAttribute("style","float:left;padding-left:2px;");
				_a.textContent =jsonData.text;
				
				_div.setAttribute("style","height:20px;border-top:1px solid #428bca;width:25px;height:0px;margin-top:20px;float:left;");
				_clearDiv.setAttribute("style","clear:both");
				
				_li.appendChild(_div);
				_li.appendChild(_a);
				_li.appendChild(_clearDiv);
				
				_ul.appendChild(_li);
				
				if(!jQuery.isEmptyObject(jsonData.params) ){
					menu.setParams(_a,jsonData.params);
				}
				
				$(_a).on("click",function(){
					menu.itemClick(this,params);
					params.backFunction(this);
					
				});
				
				if(jsonData.children != undefined){
					var result = menu.createItemDocument(jsonData.children,"15",params);
					_li.appendChild(result);
					
				}
				
			}
			return _ul;
		},
		itemClick: function(obj,params){
			var $child;
			$child = $(obj).parent().children(".item_menu");
			menu.reloadMenuStyle(params);
			
			$(obj).css("background","#428bca");
			$(obj).css("color","white");
			$(obj).addClass("menu-active");
			
			$child.each(function(index){
				if($(this).is("ul") && $(this).css("display") == "none"){
					$(this).show(200);
				}else if($(this).is("ul") && $(this).css("display") == "block"){
					$(this).hide(200);
				}
			});
			if($(obj).attr("a-href") != undefined && $(obj).attr("a-href") != ""){
				$("#"+params.loadPageId).load($(obj).attr("a-href"));
			}
		},
		setParams: function(obj,data){
			for(var key in data){
				$(obj).attr(key,data[key]);
			}
		},
		reloadMenuStyle: function(params){
			var $_topMenu =  $("#"+params.menuId);
			$_topMenu.find("li,div,ul,a").each(function(){
				$(this).css("background","");
				$(this).css("color","#337ab7");
				$(this).css("-webkit-tap-highlight-color","rgba(0,0,0,0)");
				$(this).removeClass("menu-active");
			});
		},
		createMenu: function(params){
			var $_topMenu =  $("#"+params.menuId);
			var data = params.data;
			for(var i = 0;i < data.length;i++){
					var menuJson = data[i];
					var _div = menu.createDocument(menuJson,params);
					$_topMenu.append(_div);
					var topMenuArr = menuJson.children;
					$(_div).find("div[data-mark=true]").parent().append(menu.createItemDocument(topMenuArr,"25",params));
					
				}
		}
	}
	
	$.fn.extend({
		initMenu: function(options){
			var params = $.extend({
				menuId: "",
				url: "",
				data: null,
				showIndex: 1,
				loadPageId: "",
				backFunction:function(obj){
					
				}
			},options);
			menu.init(params);
		}
	});
	
})(jQuery);
